﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using WeifenLuo.WinFormsUI.Docking;

namespace Test_2D
{
    public partial class MainForm : Form
    {
        public MainForm(string[] _args)
        {
            InitializeComponent();
            args = _args;
        }

        FrameForm frameForm;
        ComLib comLib;

        private void MainForm_Load(object sender, EventArgs e)
        {
            frameForm = new FrameForm();
            frameForm.Dock = DockStyle.Top;

            frameForm.Show(this.dockPanel1);
            frameForm.Enabled = false;

            comLib = new ComLib();
            comLib.Show(this.dockPanel1);

            if (args.Length != 0)
                openFile(args[0]);
            //comlib.Enabled = false;
        }

        /*void comlib_Delete(int index)
        {
            comList.RemoveAt(index);
            comLib.RefreshList();
            //throw new NotImplementedException();
        }*/


        SourceFile sourceFile;
        Animation animation;
        List<Component> comList;

        EditForm editForm;
        PlayForm playForm;
        string[] args;

        const string defname = "无标题";    //默认文件名
        string filepath, filename;
        int i = 0;


        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditForm ef = new EditForm();
            ef.FileName = defname + ++i;
            ef.FrameForm = this.frameForm;
            ef.Show(this.dockPanel1, true);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "sky|*.sky|All Files|*.*";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openFile(openFileDialog1.FileName);
            }
        }
        private void openFile(string _filePath)
        {
            try
            {
                filepath = _filePath;

                FileStream fs = new FileStream(filepath, FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                SourceFile sourceFile = (SourceFile)formatter.Deserialize(fs);
                fs.Close();

                EditForm ef = new EditForm(sourceFile);
                ef.FilePath = filepath;
                ef.Show(this.dockPanel1, true);
            }
            catch (Exception)
            {
                MessageBox.Show("未知文件");
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editForm.FilePath != "")
            {
                FileStream fs = new FileStream(editForm.FilePath, FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, editForm.SourceFile);
                fs.Close();
            }
            else
            {
                另存为ToolStripMenuItem_Click(sender, e);
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "sky|*.sky|All Files|*.*";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, editForm.SourceFile);

                editForm.FilePath = saveFileDialog1.FileName;
                editForm.TabText = editForm.FileName;

                fs.Close();
            }
        }

        private void 导入元件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCom addcom = new AddCom();
            addcom.ShowDialog();
            if (addcom.newcom == true)
            {
                comLib.Add(addcom.Component);
            }
            addcom.Dispose();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void 画布设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CanvasSize cs = new CanvasSize(editForm.Canvas);
            cs.ShowDialog();
            if (cs.Change == true)
            {
                editForm.Canvas = cs.Canvas;
                cs.Dispose();
            }
        }

        private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["playForm"] == null)
            {
                playForm = new PlayForm(editForm.Canvas, editForm.Animation);
                playForm.Show();
            }
        }

        private void 时间轴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frameForm.Show(dockPanel1);
        }

        private void 元件库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            comLib.Show(dockPanel1);
        }

        //编辑窗体切换
        private void dockPanel1_ActiveDocumentChanged(object sender, EventArgs e)
        {

            if (((DockPanel)sender).DocumentsCount == 0)
            {
                frameForm.Clear();
                comLib.Clear();
                enableElements(false);

                return;
            }
            else
            {

                EditForm ef = (EditForm)((DockPanel)sender).ActiveDocument;
                if (ef != null)
                {
                    enableElements(true);

                    editForm = ef;
                    frameForm.Animation = editForm.Animation;
                    comLib.ComList = editForm.ComList;

                    this.frameForm.Change += new Frame_Change(editForm.frame_Change);   //在FrameForm中会清除上一个委托

                    editForm.TabText = editForm.FileName;
                    frameForm.FrameDate = editForm.FrameData;
                    editForm.FrameForm = frameForm;
                }
            }
        }

        private void enableElements(bool enable)
        {
            frameForm.Enabled = enable;
            comLib.Enabled = enable;
            保存ToolStripMenuItem.Enabled = enable;
            另存为ToolStripMenuItem.Enabled = enable;
            导入元件ToolStripMenuItem.Enabled = enable;

            画布设置ToolStripMenuItem.Enabled = enable;
            播放ToolStripMenuItem.Enabled = enable;
        }
    }
}